package com.glsc.ngateway.platform.controller.costallocation;

import com.glsc.ngateway.common.api.ldap.dto.LdapEhrDeptDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.domain.costallocation.BusiSystemInfo;
import com.glsc.ngateway.platform.domain.costallocation.SystemComponentCategory;
import com.glsc.ngateway.platform.dto.AllDepDto;
import com.glsc.ngateway.platform.dto.BusiSystemInfoDto;
import com.glsc.ngateway.platform.dto.BusiSystemInfoQueryDto;
import com.glsc.ngateway.platform.dto.DepartmentDto;
import com.glsc.ngateway.platform.service.costallocation.BusiSystemInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guoj
 * @description: 成本分摊-系统信息
 * @date 2024/7/18 13:43
 */
@RestController
@RequestMapping("/busisystemInfo")
@Api(value = "BusiSystemInfoController", tags = {"系统信息"})
public class BusiSystemInfoController {

    @Autowired
    private BusiSystemInfoService busiSystemInfoService;


    @PostMapping("/listpage")
    @ApiOperation(value = "查询用户分页")
    public PlatformResponse<Page<BusiSystemInfo>> listpage(@ApiParam(value = "查询条件") @RequestBody BusiSystemInfoQueryDto busiSystemInfoQueryDto) {
        return PlatformResponse.successData(busiSystemInfoService.findPage(busiSystemInfoQueryDto));
    }

    @PostMapping("/findAllForSelect")
    @ApiOperation(value = "查询全部下拉")
    public PlatformResponse<List<BusiSystemInfo>> findAllForSelect() {
        return PlatformResponse.successData(busiSystemInfoService.findAllForSelect());
    }


    @PostMapping("/listSCCpage")
    @ApiOperation(value = "查询应用类别分页")
    public PlatformResponse<Page<BusiSystemInfo>> listSCCpage(@ApiParam(value = "查询条件") @RequestBody BusiSystemInfoQueryDto busiSystemInfoQueryDto) {
        return PlatformResponse.successData(busiSystemInfoService.findSCCPage(busiSystemInfoQueryDto));
    }

    @GetMapping("/findByCurrUser")
    @ApiOperation(value = "查询当前用户所属系统")
    public PlatformResponse<List<BusiSystemInfo>> findByCurrUser() {
        return PlatformResponse.successData(busiSystemInfoService.findByCurrUser());
    }

    @PostMapping("/saveSystemComponentCategory")
    @ApiOperation(value = "保存应用类别")
    public PlatformResponse<?> saveSystemComponentCategory(@RequestBody List<SystemComponentCategory> systemComponentCategorylist) {
       busiSystemInfoService.saveSystemComponentCategory(systemComponentCategorylist);
        return PlatformResponse.success();
    }

    @PostMapping("/deleteSystemComponentCategory")
    @ApiOperation(value = "删除应用类别")
    public PlatformResponse<?> deleteSystemComponentCategory(@RequestBody List<SystemComponentCategory> toDeleteList) {
        busiSystemInfoService.deleteSystemComponentCategory(toDeleteList);
        return PlatformResponse.success();
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存分摊比例")
    public PlatformResponse<?> save(@RequestBody BusiSystemInfoDto busiSystemInfoDto){
        BusiSystemInfo saved = busiSystemInfoService.checkBusiSystemInfo(busiSystemInfoDto);
        busiSystemInfoService.saveCurrentAndHistory(busiSystemInfoDto,saved);
        return PlatformResponse.success();
    }

    @SneakyThrows
    @RequestMapping(method = RequestMethod.GET, value = "getDepartmentOneUsers")
    public PlatformResponse<List<DepartmentDto>> getDepartmentOneUsers(){
        return PlatformResponse.successData(busiSystemInfoService.getDepartmentOneUsers());
    }

    @SneakyThrows
    @RequestMapping(method = RequestMethod.GET, value = "getAllDepartmentOne")
    public PlatformResponse<List<LdapEhrDeptDto>> getDepartmentList(){
        return PlatformResponse.successData(busiSystemInfoService.convertToDepList());
    }

    @SneakyThrows
    @RequestMapping(method = RequestMethod.GET, value = "getAllDepWithChildren")
    @ApiOperation(value = "获取公司和对应一级部门")
    public PlatformResponse<AllDepDto> getAllDepWithChrild(){
        ArrayList<AllDepDto> allDepWithChildren = busiSystemInfoService.getAllDepWithChildren();
        AllDepDto root = new AllDepDto();
        root.setKey("国联证券股份有限公司");
        root.setTitle("国联证券股份有限公司");
        root.setChildren(allDepWithChildren);
        return PlatformResponse.successData(root);
    }

    @PostMapping("/saveMultipleDep")
    @ApiOperation(value = "保存分摊比例-多部门")
    public PlatformResponse<?> saveAllDep(@RequestBody BusiSystemInfoDto busiSystemInfoDto){
        BusiSystemInfo saved = busiSystemInfoService.checkBusiSystemInfo4MultipleDep(busiSystemInfoDto);
        busiSystemInfoService.checkBusiSystemInfo4MultipleDep(busiSystemInfoDto,saved);
        return PlatformResponse.success();
    }

    
}
